home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / misc / Radio / radio / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-14  |  6.1 KB  |  388 lines

  1. /*
  2.  * Error handling routines from Stevens: UNIX network programming
  3.  * pp 722-731
  4.  */
  5.  
  6. /*
  7.  * Error handling routines.
  8.  *
  9.  * The functions in this file are independent of any application
  10.  * variables, and may be used in any C program.  Either of the names
  11.  * CLIENT of SERVER may be defined when compiling this function.
  12.  * If neither are defined, we assume SERVER.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <varargs.h>
  17. #include "systype.h"
  18.  
  19. #ifdef CLIENT
  20. #ifdef SERVER
  21. /*no way jose, can't define both! (CLIENT and SERVER)*/
  22. #endif
  23. #endif
  24.  
  25. #ifndef CLIENT
  26. #ifndef SERVER
  27. #define CLIENT 1
  28. #endif
  29. #endif
  30.  
  31. #ifndef NULL
  32. #define NULL ((void *) 0)
  33. #endif
  34.  
  35. char *pname = NULL;
  36.  
  37. #ifdef CLIENT  /* these all output to stderr */
  38.  
  39. /*
  40.  * Fatal error.  Print a message and terminate.
  41.  * Don't dump core and don't print the system's errno value.
  42.  *
  43.  *     err_quit(str, arg1, arg2, .....)
  44.  *
  45.  * The string "str" must specify the conversion specification for any args.
  46.  *
  47.  */
  48.  
  49. /*VARARGS1*/
  50. err_quit(va_alist)
  51. va_dcl
  52. {
  53.     va_list args;
  54.     char *fmt;
  55.  
  56.     va_start(args);
  57.     if (pname != NULL)
  58.         fprintf(stderr, "%s: ",pname);
  59.     fmt = va_arg(args, char *);
  60.  
  61. #ifndef NO_VPRINTF
  62.     vprintf(stderr, fmt, args);
  63. #endif
  64.     fputc('\n', stderr);
  65.     va_end(args);
  66.  
  67.     exit(1);
  68. }
  69.  
  70. /*
  71.  * Fatal error related to a system call. Print the message and terminate.
  72.  * Don't dump core, but do print the systems errno value and its
  73.  * associated message.
  74.  *
  75.  *    err_sys(str, arg1, arg2, ...)
  76.  *
  77.  */
  78.  
  79. /*VARARGS1*/
  80. err_sys(va_alist)
  81. va_dcl
  82. {
  83.     va_list args;
  84.     char *fmt;
  85.  
  86.     va_start(args);
  87.     if (pname != NULL)
  88.         fprintf(stderr, "%s: ", pname);
  89.     fmt = va_arg(args, char *);
  90.  
  91. #ifndef NO_VPRINTF
  92.     vprintf(stderr, fmt, args);
  93. #endif
  94.  
  95.     va_end(args);
  96.  
  97.     my_perror();
  98.  
  99.     exit(1);
  100. }
  101.  
  102.  
  103. /*
  104.  * Recoverable error. print a message and return to the caller.
  105.  *
  106.  *     err_ret(str, arg1, arg2, ...)
  107.  */
  108.  
  109. /*VARARGS1*/
  110. err_ret(va_alist)
  111. va_dcl
  112. {
  113.     va_list    args;
  114.     char    *fmt;
  115.  
  116.     va_start(args);
  117.     if (pname != NULL)
  118.         fprintf(stderr, "%s: ", pname);
  119.     fmt = va_arg(args, char *);
  120.  
  121. #ifndef NO_VPRINTF
  122.     vprintf(stderr, fmt, args);
  123. #endif
  124.  
  125.     va_end(args);
  126.  
  127.     my_perror();
  128.  
  129.     fflush(stdout);
  130.     fflush(stderr);
  131.     return;
  132. }
  133.  
  134.  
  135. /*
  136.  * Fatal error.  Print a message, dump core and terminate.
  137.  *
  138.  *    err_dump(str, arg1, arg2, ....)
  139.  *
  140.  */
  141.  
  142. /*VARARGS1*/
  143. err_dump(va_alist)
  144. va_dcl
  145. {
  146.     va_list args;
  147.     char *fmt;
  148.  
  149.     va_start(args);
  150.     if (pname != NULL)
  151.         fprintf(stderr, "%s: ", pname);
  152.     fmt = va_arg(args, char *);
  153.  
  154. #ifndef NO_VPRINTF
  155.     vprintf(stderr, fmt, args);
  156. #endif
  157.  
  158.     va_end(args);
  159.  
  160.     my_perror();
  161.  
  162.     fflush(stdout);        /* abort doesn't flush stdio buffers */
  163.     fflush(stderr);
  164.  
  165.     abort();        /* dump core and terminate */
  166.     exit(1);
  167. }
  168.  
  169. /*
  170.  * Print the UNIX errno value
  171.  */
  172.  
  173.  
  174. my_perror()
  175. {
  176.     char *sys_err_str();
  177.  
  178.     fprintf(stderr, " %s\n", sys_err_str());
  179. }
  180.  
  181. #endif /* CLIENT */
  182.  
  183. #ifdef SERVER
  184.  
  185. #ifdef BSD
  186.  /*
  187.   * Under BSD, these server routines use the syslog(3) facility.
  188.   * They don't append a newline for example.
  189.   */
  190.  
  191. #include <syslog.h>
  192.  
  193.  
  194. #else /* not BSD */
  195. /*
  196.  * There really ought to be a better way to handle server logging
  197.  * under System V
  198.  */
  199.  
  200. #define syslog(a,b)    fprintf(stderr, "%s\n", (b))
  201. #define openlog(a,b,c)    fprintf(stderr, "%s\n", (a))
  202.  
  203. #endif  /* BSD */
  204.  
  205. char emesgst[255] = {0};  /* used by all server routines */
  206.  
  207. /*
  208.  * Identify ourselves, for syslog() messages.
  209.  *
  210.  * LOG_PID is an option that says prepend each message with our pid.
  211.  * LOG_CONS is an option that says write to the console if unable to
  212.  * send the message to syslogd.
  213.  * LOG_DAEMON is our facility.
  214.  */
  215.  
  216. err_init(ident)
  217. char *ident);
  218. {
  219.     openlog(ident, (LOG_PID|LOG_CONS), LOG_DAEMON);
  220. }
  221.  
  222. /*
  223.  * Fatal error.  Print a message and terminate.
  224.  * Don't print the system's errno value.
  225.  *
  226.  *    err_quit(str, arg1, arg2, ...)
  227.  */
  228.  
  229. /*VARARGS1*/
  230. err_quit(va_alist)
  231. va_dcl
  232. {
  233.     va_list args;
  234.     char *fmt;
  235.  
  236.     va_start(args);
  237.     fmt = va_arg(args, char *);
  238.     vsprintf(emesgstr, fmt, args);
  239.     va_end(args);
  240.  
  241.     syslog(LOG_ERR, emesgstr);
  242.  
  243.     exit(1)
  244. }
  245.  
  246.  
  247. /*
  248.  * Fatal error related to a system call.  Print the message and terminate.
  249.  * Don't dump core, but do print the systems errno value and its associated
  250.  * message.
  251.  */
  252.  
  253. /*VARARGS*/
  254. err_sys(va_alist)
  255. va_dcl
  256. {
  257.     va_list args;
  258.     char *fmt;
  259.  
  260.     va_start(args);
  261.     fmt = va_arg(args, char *);
  262.     vsprintf(emesgstr, fmt, args);
  263.     va_end(args);
  264.  
  265.     my_perror();
  266.     syslog(LOG_ERR, emesgstr);
  267.  
  268.     exit(1);
  269. }
  270.  
  271.  
  272. /*
  273.  * Recoverable error.  Print a message, and return to caller.
  274.  */
  275.  
  276. /*VARARGS1*/
  277. err_ret(va_alist)
  278. va_dcl
  279. {
  280.     va_alist args;
  281.     char *fmt;
  282.  
  283.     va_start(args);
  284.     fmt = va_arg(args, char *);
  285.     vsprintf(emergstr, fmt, args);
  286.     va_end(args);
  287.  
  288.     my_perror();
  289.     syslog(LOG_ERR, emergstr);
  290.  
  291.     return;
  292. }
  293.  
  294.  
  295. /*
  296.  * Fatal error.  Print a message, dump core and terminate.
  297.  */
  298.  
  299. /*VARARGS1*/
  300. err_dump(va_alist)
  301. va_dcl
  302. {
  303.     va_list args;
  304.     char *fmt;
  305.  
  306.     va_start(args);
  307.     fmt = va_arg(args, char *);
  308.     vsprintf(emesgstr, fmt, args);
  309.     va_end(args);
  310.  
  311.     my_perror();
  312.     syslog(LOG_ERR, emesgstr);
  313.  
  314.     abort();
  315.     exit(1);
  316. }
  317.  
  318. /*
  319.  * Print the UNIX errno value.
  320.  * We just append it the the end of the emesgstr[] array.
  321.  */
  322.  
  323. void
  324. my_perror()
  325. {
  326.     int len;
  327.     char *sys_err_str();
  328.  
  329.     len = strlen(emesgstr);
  330.     sprintf(emesgstr + len, " %s", sys_err_str());
  331. }
  332.  
  333. #endif  /* SERVER */
  334.  
  335.  
  336. extern int errno;        /* UNIX error number */
  337. extern int sys_nerr;        /* # of error message strings in sys table */
  338. extern char *sys_errlist[];    /* the system error message table */
  339.  
  340. #ifdef SYS5
  341. int t_errno;
  342. int t_nerr;
  343. char *t_errlist[1];
  344. #endif
  345.  
  346. /*
  347.  * Return a string containing some additional operating system
  348.  * dependent information.
  349.  * Note that different versions of UNIX assign different meanings
  350.  * to the same value of "errno".  This means that if an error
  351.  * condition is being sent ot another UNIX system, we must interpret
  352.  * the errno value on the system that generated the error, and not
  353.  * just send the decimal value of errno to the other system.
  354.  */
  355.  
  356. char *
  357. sys_err_str()
  358. {
  359.     static char msgstr[200];
  360.  
  361.     if (errno != 0) {
  362.         if (errno >0 && errno < sys_nerr)
  363.             sprintf(msgstr, "(%s)", sys_errlist[errno]);
  364.         else
  365.             sprintf(msgstr, "(errno = %d)", errno);
  366.     } else {
  367.         msgstr[0] = '\0';
  368.     }
  369.  
  370. #ifdef SYS5
  371.  
  372.     if (t_errno != 0) {
  373.         char tmsgstr[100];
  374.  
  375.         if (t_errno > 0 && t_errno < sys_nerr)
  376.             sprintf(tmsgstr, " (%s)", t_errlist[t_errno]);
  377.         else
  378.             sprintf(tmsgstr, ", (t_errno = %d)", t_errno);
  379.  
  380.         strcat(msgstr, tmsgstr);
  381.     }
  382. #endif
  383.  
  384.     return(msgstr);
  385. }
  386.  
  387.  
  388.